/*===============================================
				Address
===============================================*/
var cityinfo = 
{
	"全国":		["北京市","长沙市", "大连市", "武汉市", "成都市"],
	"全国s":	["北京", "长沙", "大连", "武汉","成都"],
	"北京市":	["西城区", "崇文区", "宣武区", "朝阳区", "丰台区", "石景山区", "海淀区", "门头沟区", "房山区", "通州区", "顺义区", "昌平区", "大兴区", "怀柔区", "平谷区", "密云县", "延庆县"],
	"北京市s":	["西城", "崇文", "宣武", "朝阳", "丰台", "石景山", "海淀", "门头沟", "房山", "通州", "顺义", "昌平", "大兴", "怀柔", "平谷", "密云", "延庆"],
	"大连市":	["中山区", "西岗区", "沙河口区", "甘井子区", "开发区", "旅顺口区", "金州区", "长海县"],
	"大连市s":	["中山", "西岗", "沙河口", "甘井子", "开发", "旅顺口", "金州", "长海"],
	"长沙市":	["天心区", "岳麓区", "开福区", "雨花区", "望城县", "宁乡县"],
	"长沙市s":	["天心", "岳麓", "开福", "雨花", "望城", "宁乡"],
	"武汉市":	["江岸区", "江汉区", "硚口区", "汉阳区", "武昌区", "青山区", "洪山区", "东西湖区", "汉南区", "蔡甸区", "黄陂区", "新洲区"],
	"武汉市s":	["江岸", "江汉", "硚口", "汉阳", "武昌", "青山", "洪山", "东西湖", "汉南", "蔡甸", "黄陂", "新洲"],
	"成都市":	["锦江区", "青羊区", "金牛区", "武侯区", "成华区", "龙泉驿区", "青白江区", "新都区", "温江区"],
	"成都市s":	["锦江", "青羊", "金牛", "武侯", "成华", "龙泉驿", "青白江", "新都", "温江"],

	"prefix":	["区", "县", "门外大街", "大街","百货","市场", "胡同", "站","街","号", "路", "桥", "道", "村", "楼"],
	"no":		["世界"]
};

//-去掉特殊前缀-
function getShortName(city, strSrc)
{
	//-判断是否包含前缀-
	function isIncludePrefix(src, prefix, bRightToLeft)
	{
		var bInclude = false;
		if(src.length>=prefix.length)
		{
			var strSub;
			if(bRightToLeft)			
				strSub = src.substr(src.length-prefix.length);
			else
				strSub = src.substr(0, prefix.length);
			if(strSub==prefix)
				bInclude = true;
		}
		return bInclude;
	}

	//-如果前缀存在,则去掉前缀-
	function removePrefix(src, prefix, bRightToLeft)
	{
		var rtn;
		if(bRightToLeft)
			rtn = src.substr(0, src.length - prefix.length);
		else
			rtn = src.substr(prefix.length);
		return rtn;
	}

	//-去掉存在于某组中的前后缀-
	function removePrefixForA(src, cityarray, bRightToLeft)
	{
		var rtn = src;
		var bInclude = false;
		for(var i=0; i<cityarray.length; i++)
		{
			bInclude = isIncludePrefix(src, cityarray[i], bRightToLeft);
			if(bInclude)
			{
				rtn = removePrefix(src, cityarray[i], bRightToLeft);
				break;
			}
		}

		return rtn;
	}
	
	//0.字数太短直接返回
	var rtn = "";
	var nLenSrc = strSrc.length;
	if(nLenSrc<2)
		return "";
	
	//0.1 去掉数字后再判断
	rtn = strSrc.replace(/\d+/g, "");
	if(rtn.length<2)
		return "";

	//1.先去掉城市
	strSrc = rtn;
	nLenSrc = strSrc.length;
	rtn = removePrefixForA(strSrc, cityinfo["全国"]);
	if(nLenSrc==rtn.length)
	{
		rtn = removePrefixForA(strSrc, cityinfo["全国s"]);		
		if(rtn.length<2)
			return "";
	}

	//2. 再去掉区
	strSrc = rtn;
	nLenSrc = strSrc.length;
	rtn = removePrefixForA(strSrc, cityinfo[city]);
	if(nLenSrc==rtn.length)
	{
		rtn = removePrefixForA(strSrc, cityinfo[city+"s"]);
		if(rtn.length<2)
			return "";	
	}

	//3.再去掉可能重复前缀
	while(1)
	{
		strSrc = rtn;
		nLenSrc = strSrc.length;
		rtn = removePrefixForA(strSrc, cityinfo["prefix"]);
		if(rtn.length<2)
			return "";

		if(rtn.length==nLenSrc)
			break;
	}

	if(rtn.length<2)
		return "";

	//4.对于很短的,判断最后结尾
	if(rtn.length<4)
	{
		strSrc = rtn;
		nLenSrc = strSrc.length;
		rtn = removePrefixForA(strSrc, cityinfo["prefix"], true);
		if(rtn.length<2)
			return "";

		strSrc = rtn;
		nLenSrc = strSrc.length;
		rtn = removePrefixForA(strSrc, cityinfo["no"]);
		if(rtn.length<2)
			return "";
	}

	return rtn;
}


/*===============================================
				Address Regexp
===============================================*/
var citys = 
{
	"北京市":	["北京", "西城", "崇文", "宣武", "朝阳", "丰台", "石景山", "海淀", "门头沟", "房山", "通州", "顺义", "昌平", "大兴", "怀柔", "平谷", "密云", "延庆"],
	"大连市":	["大连", "中山", "西岗", "沙河口", "甘井子", "开发", "旅顺口", "金州", "长海"],
	"长沙市":	["长沙", "天心", "岳麓", "开福", "雨花", "望城", "宁乡"],
	"武汉市":	["武汉", "江岸", "江汉", "硚口", "汉阳", "武昌", "青山", "洪山", "东西湖", "汉南", "蔡甸", "黄陂", "新洲"],
	"成都市":	["成都", "锦江", "青羊", "金牛", "武侯", "成华", "龙泉驿", "青白江", "新都", "温江"],

	"no":	["世界"]
};

//-获取区县-
function sGetQuxian(strCity, nStart)
{
	var strRtn = "";
	var oSet = citys[strCity];
	if (typeof(oSet)!="object")
		return strRtn;

	for(var i=nStart; i<oSet.length; i++)
	{
		if(oSet[i]=="")
			continue;
		if(strRtn!="" )
			strRtn += "|";	
		strRtn = strRtn + "(" + oSet[i] + ")";	
	}
	
	return strRtn;
}

//-获取"提取区县"正则表达式-
function citys_getregexp_pickQuxian(strCityName)
{
	function sGetRegexp_pickquxian(strCity)
	{
		var strRtn = "";
		var strQuxian=sGetQuxian(strCity, 1);
		if(strQuxian=="")
			return strRtn;

		//最后表达式
		var g_template = "({0})[区|县]?"
		var strRtn = g_template.format(strQuxian)
		return strRtn;
	}

	var strKey = strCityName + "_pickquxian";
	var strRegexp = citys[strKey];
	if(!strRegexp || strRegexp=="")
	{
		strRegexp = sGetRegexp_pickquxian(strCityName);
		if(strRegexp!="")
			citys[strKey] = strRegexp;
	}

	return strRegexp;
}

//-获取"提取短地址"正则表达式-
function citys_getregexp_shortAddress(strCityName)
{
	function sGetRegexp_shortaddress(strCity)
	{
		var strRtn = "";
		var strQuxian=sGetQuxian(strCity, 0);
		if(strQuxian=="")
			return strRtn;

		//最后表达式
		var g_template = "(近郊)?({0})[市|区|县]?"
		var strRtn = g_template.format(strQuxian)
		return strRtn;
	}

	var strKey = strCityName + "_shortaddress";
	var strRegexp = citys[strKey];
	if(!strRegexp || strRegexp=="")
	{
		strRegexp = sGetRegexp_shortaddress(strCityName);
		if(strRegexp!="")
			citys[strKey] = strRegexp;
	}

	return strRegexp;
}

//-提取地址中的区县-
function Address_GetQuxian(strCity, strAddress)
{
	var strRegexp = citys_getregexp_pickQuxian(strCity);
	var strQuxian = RegexpPick(strRegexp, strAddress, 1);
	return strQuxian;
}

//-获取短地址-
function Address_GetShort(strCity, strAddress)
{
	var strRegexp = citys_getregexp_shortAddress(strCity);
	var strQuxian = RegexpReplace(strRegexp, strAddress, "");
	return strQuxian;
}

//-对地址重复部分进行精简判断-
var c_strAddressPattern0A = "^(大楼)|(大厦)|(大街)|(广场)|(小区)|(超市)|(胡同)|(中心)|(B1)|(百货)|(大学)$";
var c_strAddressPattern0B = "^(号楼)|(对面)|(城区)|(东路)|(西侧)|(路口)|(路甲)|(路南)|(底商)|(站南)|(里甲)|(园路)$";
var c_strAddressPattern0C = "^(美食城)|(园小区)|(0米路)$";
var c_strAddressPattern0D = "^购物中心$";
var c_strAddressPattern1 = "^[\dABCD-]{1,4}[号楼层米座]?$";
var c_strAddressPattern2 = "^[号路街甲楼层座区].$";
var c_strAddressPattern3 = "^[\d-]{1,3}[楼号]\d?$";
var c_strAddressPattern4 = "^[东南西北桥][门路东南西北]\d?$";
var c_strAddressPattern5 = "^\d{1,3}号.{1,2}$";
var c_strAddressPattern6 = "^.?门[内外]大街\d?$";
var c_strAddressPattern7 = "^[区路]甲?\d{1,3}号$";
var c_strAddressPattern8 = "^((广场)|(中心)|路|甲|座|(号楼)|(大厦))[ABCD]?\d{1,3}楼?$";

function LCS_Address(strAddressSrc, strAddressDest)
{
    var strRtn = "";
    
    var strSame = LCS(strAddressDest, strAddressSrc);

    var nLen = strSame.length;
    if (nLen <= 1) return strRtn;
    
    //正则表达式
    var bMatch  = false;
    if (nLen==2)
	{
        bMatch = RegexpMatch(c_strAddressPattern0A, strSame);
        if(bMatch) return strRtn;
        bMatch = RegexpMatch(c_strAddressPattern0B, strSame);
        if(bMatch) return strRtn;
    }    

    if (nLen == 3)
	{
        bMatch = RegexpMatch(c_strAddressPattern0C, strSame);
        if(bMatch) return strRtn;
    } 
	
    if (nLen == 4) 
	{
        bMatch = RegexpMatch(c_strAddressPattern0D, strSame);
        if(bMatch) return strRtn;
    }
    
    if (nLen <= 5)
	{
        bMatch = RegexpMatch(c_strAddressPattern1, strSame);
        if(bMatch) return strRtn;
	}
   
    if (nLen == 2)
	{
        bMatch = RegexpMatch(c_strAddressPattern2, strSame);
        if(bMatch) return strRtn;
    }
   
    if (nLen <= 5)
	{
        bMatch = RegexpMatch(c_strAddressPattern3, strSame);
        if(bMatch) return strRtn;
    }
   
    if (nLen <= 3)
	{
        bMatch = RegexpMatch(c_strAddressPattern4, strSame);
        if(bMatch) return strRtn;
    }
   
    if (nLen >= 3 && nLen <= 6)
	{
        bMatch = RegexpMatch(c_strAddressPattern5, strSame);
        if(bMatch) return strRtn;
    }
   
    if (nLen >= 3 && nLen <= 5)
	{
        bMatch = RegexpMatch(c_strAddressPattern6, strSame);
        if(bMatch) return strRtn;
    }
    
    if (nLen >= 3 && nLen <= 5)
	{
        bMatch = RegexpMatch(c_strAddressPattern7, strSame);
        if(bMatch) return strRtn;
    }    

    if (nLen >= 3 && nLen <= 5)
	{
        bMatch = RegexpMatch(c_strAddressPattern8, strSame);
        if(bMatch) return strRtn;
	}

    strRtn = strSame;
	return strRtn;
}

//判断两个地址是否相同 
function LCS_Address_Ex(strCity, strAddressSrc, strAddressDest)
{
//*
	//-如果区县不一致，返回-
	var	strRtn = "";
	var strQuxianSrc  = Address_GetQuxian(strCity, strAddressSrc);
	var strQuxianDest = Address_GetQuxian(strCity, strAddressDest);
	if(strQuxianSrc!="" && strQuxianDest!="" && strQuxianSrc!=strQuxianDest)
		return strRtn;

	//-去除区县比较-
	var strShortSrc  = Address_GetShort(strCity, strAddressSrc);
	var strShortDest = Address_GetShort(strCity, strAddressDest);
	if(strShortSrc=="" || strShortDest=="")
		return strRtn;

	strRtn = LCS_Address(strShortSrc, strShortDest);
	return strRtn;	
//*/
}


/*===============================================
				Fandian
===============================================*/
var c_strFandianPatternSplit = "\(|\)";			//拆分
var c_strFandianPattern = "^(大道)|(武汉)$";
var c_strFandianPattern2 = "^.店$";
function SplitFandian(strFandianAll)
{
	//var re = new RegExp(c_strFandianPatternSplit, "gi");	//why failed?
	var re = /\(|\)/g;
	var rtn = strFandianAll.split(re,2);
	return rtn;
}


function LCS_Fandian(strFandian1, strFandian2)
{
	var bSame = false;

	if(strFandian1==strFandian2)
	{
		bSame = true;
		return bSame;
	}

	var rtn1 = SplitFandian(strFandian1);
	var rtn2 = SplitFandian(strFandian2);
	if(rtn1.length!=2 || rtn2.length!=2)
		return bSame;

	//1. 如果名称不匹配，放弃
	var strSame = "";
	var bMatch = false;
	if(rtn1[0]!=rtn2[0])
	{
		strSame = LCS(rtn1[0], rtn2[0]);
		if(strSame.length<=1)
			return bSame;

		if(strSame.length==2)
		{
			bMatch = RegexpMatch(c_strFandianPattern2, strSame);
			if(bMatch)
				return bSame;
		}
	}

	//2. 如果分店不匹配，放弃
	var strSame = LCS(rtn1[1], rtn2[1]);
	if(strSame.length<=1)
		return bSame;

	if(strSame.length==2)
	{
		var bMatch = RegexpMatch(c_strFandianPattern, strSame);
		if(bMatch)
			return bSame;

		bMatch = RegexpMatch(c_strFandianPattern2, strSame);
		if(bMatch)
			return bSame;
	}

	bSame = true;

	return bSame;
}
